STRING(COMPARE EQUAL "${ENABLE_GTK_GUI}" "" AUTODETECT_GTK_GUI)

# For cmake < 2.8
MACRO(UNSET V)
	SET(${V})
ENDMACRO(UNSET)

SET(GTKMM_24_MIN_VERSION  2.24.0)
SET(GTKMM_30_MIN_VERSION  3.12.0)
SET(LIBNOTIFY_MIN_VERSION 0.7.2 )
IF(AUTODETECT_GTK_GUI)
	find_package(PkgConfig)

	IF(PKG_CONFIG_FOUND)

		## Check for GtkMM ##
		pkg_check_modules(GTKMM24 gtkmm-2.4>=${GTKMM_24_MIN_VERSION})
		pkg_check_modules(GTKMM30 gtkmm-3.0>=${GTKMM_30_MIN_VERSION})

		IF(GTKMM24_FOUND)
			SET(USE_GTK3x_API OFF CACHE BOOL "Enable building against the newer GTK+3 toolkit.")
		ENDIF(GTKMM24_FOUND)

		IF(GTKMM30_FOUND AND NOT GTKMM24_FOUND)
			SET(USE_GTK3x_API ON CACHE BOOL "Enable building against the newer GTK+3 toolkit.")
		ENDIF(GTKMM30_FOUND AND NOT GTKMM24_FOUND)

		IF(GTKMM24_FOUND OR GTKMM30_FOUND)
			SET(GTKMM_FOUND TRUE)
		ELSE(GTKMM24_FOUND OR GTKMM30_FOUND)
			MESSAGE(STATUS "Gtkmm not found.")
			MESSAGE(STATUS "NOTE: In order to build the gtk-gui you need either Gtkmm-2.4 (version ${GTKMM_24_MIN_VERSION} or")
			MESSAGE(STATUS "      higher) or Gtkmm-3.0 (version ${GTKMM_30_MIN_VERSION} or higher).")
		ENDIF(GTKMM24_FOUND OR GTKMM30_FOUND)

		## Check for GThread ##
		pkg_check_modules(GTHREAD gthread-2.0)
		IF(NOT GTHREAD_FOUND)
			MESSAGE(STATUS "GThread not found.")
		ENDIF(NOT GTHREAD_FOUND)

		## Check for LibNotify (optional) ##
		pkg_check_modules(LIBNOTIFY libnotify>=${LIBNOTIFY_MIN_VERSION})
		IF(LIBNOTIFY_FOUND)
			SET(ENABLE_LIBNOTIFY ON CACHE BOOL "Enable notifications via libnotify")
		ELSE(LIBNOTIFY_FOUND)
			MESSAGE(WARNING "Missing LibNotify, you won't get any notifications!")
			SET(ENABLE_LIBNOTIFY OFF CACHE BOOL "Enable notifications via libnotify")
		ENDIF(LIBNOTIFY_FOUND)

		## Finalize autodection ##
		IF(GTKMM_FOUND AND GTHREAD_FOUND)
			MESSAGE(STATUS "Enabling gtk-gui")
			SET(ENABLE_GTK_GUI ON CACHE BOOL "Enable building of the gtk gui")
			IF(USE_GTK3x_API)
				MESSAGE(STATUS "NOTE: The gtk-gui will be built against the GTK+ 3.x API.")
			ELSE(USE_GTK3x_API)
				MESSAGE(STATUS "NOTE: The gtk-gui will be built against the GTK+ 2.4 API.")
			ENDIF(USE_GTK3x_API)
		ELSE(GTKMM_FOUND AND GTHREAD_FOUND)
			MESSAGE(STATUS "Disabling gtk-gui.")
			SET(ENABLE_GTK_GUI OFF CACHE BOOL "Enable building of the gtk gui")
		ENDIF(GTKMM_FOUND AND GTHREAD_FOUND)

	ELSE(PKG_CONFIG_FOUND)
		MESSAGE(STATUS "Missing PkgConfig, cannot search for gtkmm and gthread, disabling gtk-gui")
		SET(ENABLE_GTK_GUI OFF CACHE BOOL "Enable building of the gtk gui")
	ENDIF(PKG_CONFIG_FOUND)
ENDIF(AUTODETECT_GTK_GUI)

IF(ENABLE_GTK_GUI)
	find_package(PkgConfig REQUIRED)
	IF(USE_GTK3x_API)
		pkg_check_modules(GTKMM gtkmm-3.0>=${GTKMM_30_MIN_VERSION} REQUIRED)
		SET(GTKMM_FOUND         ${GTKMM30_FOUND}         CACHE INTERNAL "Selection between GTK+2 or GTK+3")
		SET(GTKMM_LIBRARIES     ${GTKMM30_LIBRARIES}     CACHE INTERNAL "Selection between GTK+2 or GTK+3")
		SET(GTKMM_LIBRARY_DIRS  ${GTKMM30_LIBRARY_DIRS}  CACHE INTERNAL "Selection between GTK+2 or GTK+3")
		SET(GTKMM_LDFLAGS       ${GTKMM30_LDFLAGS}       CACHE INTERNAL "Selection between GTK+2 or GTK+3")
		SET(GTKMM_LDFLAGS_OTHER ${GTKMM30_LDFLAGS_OTHER} CACHE INTERNAL "Selection between GTK+2 or GTK+3")
		SET(GTKMM_INCLUDE_DIRS  ${GTKMM30_INCLUDE_DIRS}  CACHE INTERNAL "Selection between GTK+2 or GTK+3")
		SET(GTKMM_CFLAGS        ${GTKMM30_CFLAGS}        CACHE INTERNAL "Selection between GTK+2 or GTK+3")
		SET(GTKMM_CFLAGS_OTHER  ${GTKMM30_CFLAGS_OTHER}  CACHE INTERNAL "Selection between GTK+2 or GTK+3")
	ELSE(USE_GTK3x_API)
		pkg_check_modules(GTKMM gtkmm-2.4>=${GTKMM_24_MIN_VERSION} REQUIRED)
		SET(GTKMM_FOUND         ${GTKMM24_FOUND}         CACHE INTERNAL "Selection between GTK+2 or GTK+3")
		SET(GTKMM_LIBRARIES     ${GTKMM24_LIBRARIES}     CACHE INTERNAL "Selection between GTK+2 or GTK+3")
		SET(GTKMM_LIBRARY_DIRS  ${GTKMM24_LIBRARY_DIRS}  CACHE INTERNAL "Selection between GTK+2 or GTK+3")
		SET(GTKMM_LDFLAGS       ${GTKMM24_LDFLAGS}       CACHE INTERNAL "Selection between GTK+2 or GTK+3")
		SET(GTKMM_LDFLAGS_OTHER ${GTKMM24_LDFLAGS_OTHER} CACHE INTERNAL "Selection between GTK+2 or GTK+3")
		SET(GTKMM_INCLUDE_DIRS  ${GTKMM24_INCLUDE_DIRS}  CACHE INTERNAL "Selection between GTK+2 or GTK+3")
		SET(GTKMM_CFLAGS        ${GTKMM24_CFLAGS}        CACHE INTERNAL "Selection between GTK+2 or GTK+3")
		SET(GTKMM_CFLAGS_OTHER  ${GTKMM24_CFLAGS_OTHER}  CACHE INTERNAL "Selection between GTK+2 or GTK+3")
		LIST(APPEND GUFTT_COMPILE_DEFINITIONS "USE_GTK24_API")
	ENDIF(USE_GTK3x_API)
	pkg_check_modules(GTHREAD gthread-2.0 REQUIRED)
	pkg_check_modules(LIBNOTIFY libnotify>=${LIBNOTIFY_MIN_VERSION})

	# Should these be moved to the auto-detect phase above? They really are
	# required, so without these we probably shouldn't set 'ENABLE_GTK_GUI'.
	find_package(ImageMagick COMPONENTS convert)
	find_package(SearchUtils REQUIRED)

	GLOB_PATHS(GLOB_PATHS NOBASERESULT GLOBS "*" BASES "${CMAKE_EXTRA_LIBS_PATH}")
	FOREACH(GLOB_PATH ${GLOB_PATHS})
		IF(NOT PNG2ICO_EXECUTABLE)
			find_program(PNG2ICO_EXECUTABLE NAMES png2ico png2ico.exe PATHS "${GLOB_PATH}")
		ENDIF(NOT PNG2ICO_EXECUTABLE)
	ENDFOREACH(GLOB_PATH)

	MACRO(GENERATE_ICON_IMAGE SIZE BPP COLOR_COUNT)
		STRING(COMPARE NOTEQUAL "X${GENERATE_UFTT_GTK_ICON_FROM_SOURCE_PNG_INSTEAD_OF_SVG_BECAUSE_OF_B0RKEN_SVG_SUPPORT_IN_IMAGEMAGICK}X" "XX" _GENERATE_UFTT_GTK_ICON_FROM_SOURCE_PNG_INSTEAD_OF_SVG_BECAUSE_OF_B0RKEN_SVG_SUPPORT_IN_IMAGEMAGICK)
		IF(_GENERATE_UFTT_GTK_ICON_FROM_SOURCE_PNG_INSTEAD_OF_SVG_BECAUSE_OF_B0RKEN_SVG_SUPPORT_IN_IMAGEMAGICK)
			SET(_UFTT_GTK_ICON_NAME "${CMAKE_CURRENT_BINARY_DIR}/uftt_icon_${SIZE}_${BPP}_${COLOR_COUNT}.png")
		ELSE(_GENERATE_UFTT_GTK_ICON_FROM_SOURCE_PNG_INSTEAD_OF_SVG_BECAUSE_OF_B0RKEN_SVG_SUPPORT_IN_IMAGEMAGICK)
			SET(_UFTT_GTK_ICON_NAME "${CMAKE_CURRENT_BINARY_DIR}/uftt_icon_${SIZE}_${BPP}_${COLOR_COUNT}.svg")
		ENDIF(_GENERATE_UFTT_GTK_ICON_FROM_SOURCE_PNG_INSTEAD_OF_SVG_BECAUSE_OF_B0RKEN_SVG_SUPPORT_IN_IMAGEMAGICK)
		ADD_CUSTOM_COMMAND(OUTPUT "${_UFTT_GTK_ICON_NAME}" PRE_BUILD
					   COMMAND "${IMAGEMAGICK_CONVERT_EXECUTABLE}" -resize "${SIZE}"
										       -depth "${BPP}"
										       -colors "${COLOR_COUNT}"
										       "${CMAKE_CURRENT_SOURCE_DIR}/uftt_icon.png"
										       "${_UFTT_GTK_ICON_NAME}"
					   DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/uftt_icon.svg"
					  )
		SET(UFTT_GTK_ICON_DEPS ${UFTT_GTK_ICON_DEPS} "${_UFTT_GTK_ICON_NAME}")
	ENDMACRO(GENERATE_ICON_IMAGE)

	UNSET(WIN32_UFTT_GTK_ICON_RESOURCES)
	UNSET(UFTT_GTK_ICON_RESOURCE_FILE)
	SET(GENERATE_UFTT_ICON_FROM_SOURCE_PNG_INSTEAD_OF_SVG_BECAUSE_OF_B0RKEN_SVG_SUPPORT_IN_IMAGEMAGICK "" CACHE FILEPATH "Work around bugs in ImageMagick's SVG module")
	IF(WIN32)
		UNSET(UFTT_GTK_ICON_DEPS)
		FOREACH(COLOR_COUNT 0 256 16 65535) # NOTE: -colors 0 == do not use color reduction
			FOREACH(BPP 32 16 8)
				FOREACH(RESOLUTION 32x32 16x16 24x24 48x48)
					GENERATE_ICON_IMAGE(${RESOLUTION} ${BPP} ${COLOR_COUNT})
				ENDFOREACH(RESOLUTION)
			ENDFOREACH(BPP)
		ENDFOREACH(COLOR_COUNT)

		ADD_CUSTOM_COMMAND(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/uftt.ico" PREBUILD
		                   COMMAND "${PNG2ICO_EXECUTABLE}" "${CMAKE_CURRENT_BINARY_DIR}/uftt.ico"                                                  ${UFTT_GTK_ICON_DEPS}
		                   DEPENDS ${UFTT_GTK_ICON_DEPS}
		                  )
		IF(PNG2ICO_EXECUTABLE AND IMAGEMAGICK_CONVERT_EXECUTABLE)
			SET(WIN32_UFTT_GTK_ICON_RESOURCES "${CMAKE_CURRENT_BINARY_DIR}/uftt.ico" "${CMAKE_CURRENT_SOURCE_DIR}/uftt_icon.rc")
		ELSE(PNG2ICO_EXECUTABLE AND IMAGEMAGICK_CONVERT_EXECUTABLE)
			MESSAGE(STATUS "NOTE: In order to build the icons PNG2ICO_EXECUTABLE and IMAGEMAGICK_CONVERT_EXECUTABLE need to be set")
		ENDIF(PNG2ICO_EXECUTABLE AND IMAGEMAGICK_CONVERT_EXECUTABLE)
	ENDIF(WIN32)

	UNSET(GENERATED_BIN_HEADERS)
	MACRO(EMBED_BINARY BINPATH BINFILE)
		SET(_EMBED_SRC "${CMAKE_CURRENT_SOURCE_DIR}/${BINPATH}/${BINFILE}")
		SET(_EMBED_DST "${CMAKE_CURRENT_BINARY_DIR}/${BINFILE}.h")
		ADD_CUSTOM_COMMAND(
			OUTPUT  "${_EMBED_DST}" PRE_BUILD
			COMMAND bin2c -c
			        "${_EMBED_SRC}"
			        "${_EMBED_DST}"
			DEPENDS bin2c
			        "${_EMBED_SRC}"
			)
		SET(GENERATED_BIN_HEADERS ${GENERATED_BIN_HEADERS} "${_EMBED_DST}")
	ENDMACRO(EMBED_BINARY)

	EMBED_BINARY("../qt-gui/Icons/" "uftt-16x16.png")
#	EMBED_BINARY("../qt-gui/Icons/" "uftt-22x22.png") # FIXME: Does not exist yet
	EMBED_BINARY("../qt-gui/Icons/" "uftt-32x32.png")
	EMBED_BINARY("../qt-gui/Icons/" "uftt-48x48.png")

	ADD_LIBRARY(uftt-gtk
	            ${WIN32_UFTT_GTK_ICON_RESOURCES}
	            ${GENERATED_BIN_HEADERS}
	            GTKMain.h              	GTKMain.cpp
	            GTKImpl.cpp            	GTKImpl.h
	            AutoScrollingWindow.cpp	AutoScrollingWindow.h
	            ShareList.cpp          	ShareList.h
	            TaskList.cpp           	TaskList.h
	            ShowURI.cpp            	ShowURI.h
	            PreferencesDialog.cpp  	PreferencesDialog.h
	            dispatcher_marshaller.h
	            OStreamGtkTextBuffer.h
	            Notification.cpp       	Notification.h
	)

	SET(GUFTT_LINK_LIBRARIES ${GTKMM_LIBRARIES} ${GTHREAD_LIBRARIES})

	IF(ENABLE_LIBNOTIFY)
		SET(GUFTT_LINK_LIBRARIES ${GUFTT_LINK_LIBRARIES} ${LIBNOTIFY_LIBRARIES})
		INCLUDE_DIRECTORIES(${LIBNOTIFY_INCLUDE_DIRS})
		LIST(APPEND GUFTT_COMPILE_DEFINITIONS "HAVE_LIBNOTIFY")
	ENDIF(ENABLE_LIBNOTIFY)

	IF(MSVC)
		IF(MSVC80)
			find_file(GTKMM_PROPERTY_SHEET       "gtkmm-vc80-2_4.vsprops"   "$ENV{GTKMM_BASEPATH}\\MSVC\\")
			find_file(GTKMM_DEBUG_PROPERTY_SHEET "gtkmm-vc80-d-2_4.vsprops" "$ENV{GTKMM_BASEPATH}\\MSVC\\")
		ELSE(MSVC80)
		IF(MSVC90)
			find_file(GTKMM_PROPERTY_SHEET       "gtkmm-vc90-2_4.vsprops"   "$ENV{GTKMM_BASEPATH}\\MSVC\\")
			find_file(GTKMM_DEBUG_PROPERTY_SHEET "gtkmm-vc90-d-2_4.vsprops" "$ENV{GTKMM_BASEPATH}\\MSVC\\")
		ELSE(MSVC90)
			find_file(GTKMM_PROPERTY_SHEET       "gtkmm-2_4.vsprops"        "$ENV{GTKMM_BASEPATH}\\MSVC\\")
			find_file(GTKMM_DEBUG_PROPERTY_SHEET "gtkmm-d-2_4.vsprops"      "$ENV{GTKMM_BASEPATH}\\MSVC\\")
		ENDIF(MSVC90)
		ENDIF(MSVC80)

		UNSET(GTKMM_ADDITIONAL_DEPENDENCIES)
		UNSET(GTKMM_ADDITIONAL_INCLUDE_DIRECTORIES)
		UNSET(GTKMM_ADDITIONAL_LIBRARY_DIRECTORIES)
		UNSET(GTKMM_INCLUDE_DIRS)
		UNSET(GTKMM_LIBRARY_DIRS)
		UNSET(GTKMM_LIBRARY_NAMES)
		UNSET(GUFTT_LINK_LIBRARIES)
		FOREACH(BuildType debug optimized)
			IF(BuildType MATCHES debug)
				SET(_PROPERTY_SHEET ${GTKMM_DEBUG_PROPERTY_SHEET})
			ELSE(BuildType MATCHES debug)
				SET(_PROPERTY_SHEET ${GTKMM_PROPERTY_SHEET})
			ENDIF(BuildType MATCHES debug)

			file(STRINGS "${_PROPERTY_SHEET}" GTKMM_ADDITIONAL_INCLUDE_DIRECTORIES REGEX AdditionalIncludeDirectories)
			file(STRINGS "${_PROPERTY_SHEET}" GTKMM_ADDITIONAL_DEPENDENCIES REGEX AdditionalDependencies)
			file(STRINGS "${_PROPERTY_SHEET}" GTKMM_ADDITIONAL_LIBRARY_DIRECTORIES REGEX AdditionalLibraryDirectories)
			STRING(REGEX REPLACE "\\\\" "\\\\\\\\" GTKMM_BASEPATH_REGEX $ENV{GTKMM_BASEPATH})
			FOREACH(S GTKMM_ADDITIONAL_INCLUDE_DIRECTORIES GTKMM_ADDITIONAL_DEPENDENCIES GTKMM_ADDITIONAL_LIBRARY_DIRECTORIES)
				STRING(REGEX REPLACE ^.*AdditionalIncludeDirectories[^=]*=[^\"]*\"   ""                        "${S}" "${${S}}")
				STRING(REGEX REPLACE ^.*AdditionalDependencies[^=]*=[^\"]*\"         ""                        "${S}" "${${S}}")
				STRING(REGEX REPLACE ^.*AdditionalLibraryDirectories[^=]*=[^\"]*\"   ""                        "${S}" "${${S}}")
				STRING(REGEX REPLACE \"[^\"]*$                                       ""                        "${S}" "${${S}}")
				STRING(REGEX REPLACE "\\$\\(GTKMM_BASEPATH\\)"                       ${GTKMM_BASEPATH_REGEX}   "${S}" "${${S}}")
				STRING(REGEX REPLACE "\\\\;"                                         ";"                       "${S}" "${${S}}")
				STRING(REGEX REPLACE "&quot\\;"                                      ""                        "${S}" "${${S}}")
			ENDFOREACH(S)
			SET(GTKMM_INCLUDE_DIRS ${GTKMM_INCLUDE_DIRS} "${GTKMM_ADDITIONAL_INCLUDE_DIRECTORIES}")
			SET(GTKMM_LIBRARY_DIRS ${GTKMM_LIBRARY_DIRS} "${GTKMM_ADDITIONAL_LIBRARY_DIRECTORIES}")

			SEPARATE_ARGUMENTS(GTKMM_ADDITIONAL_DEPENDENCIES)
			SET(GTKMM_LIBRARY_NAMES ${GTKMM_ADDITIONAL_DEPENDENCIES})

			FOREACH(L ${GTKMM_LIBRARY_NAMES} ${GTHREAD_LIBRARIES})
				SET(GTK_LIBPATH NOTFOUND)
				FIND_LIBRARY(GTK_LIBPATH
					NAMES ${L}
					PATHS ${GTKMM_LIBRARY_DIRS} ${GTHREAD_LIBRARY_DIRS}
				)
				SET(GUFTT_LINK_LIBRARIES ${GUFTT_LINK_LIBRARIES} ${BuildType} "${GTK_LIBPATH}")
				ENDFOREACH(L)
		ENDFOREACH(BuildType)
	ENDIF(MSVC)

	INCLUDE_DIRECTORIES(${GTKMM_INCLUDE_DIRS}
	                    ${GTHREAD_INCLUDE_DIRS}
	                    ${CMAKE_CURRENT_SOURCE_DIR} # For boost/preprocessor/iteration
	                    ${CMAKE_CURRENT_BINARY_DIR} # Make sure embedded file's headers can be found
	                   )

	SET_TARGET_PROPERTIES(uftt-gtk PROPERTIES COMPILE_DEFINITIONS "${GUFTT_COMPILE_DEFINITIONS}")

	TARGET_LINK_LIBRARIES(uftt-gtk
		${GUFTT_LINK_LIBRARIES}
	)

	IF(MSVC)
		file(STRINGS "${_PROPERTY_SHEET}" GTKMM_DISABLE_SPECIFIC_WARNINGS REGEX DisableSpecificWarnings)
		STRING(REGEX REPLACE ^.*DisableSpecificWarnings[^=]*=[^\"]*\" ";"    "GTKMM_DISABLE_SPECIFIC_WARNINGS" "${GTKMM_DISABLE_SPECIFIC_WARNINGS}")
		STRING(REGEX REPLACE \"[^\"]*$                                ""     "GTKMM_DISABLE_SPECIFIC_WARNINGS" "${GTKMM_DISABLE_SPECIFIC_WARNINGS}")
		STRING(REGEX REPLACE "\\\\;"                                  ";"    "GTKMM_DISABLE_SPECIFIC_WARNINGS" "${GTKMM_DISABLE_SPECIFIC_WARNINGS}")
		STRING(REGEX REPLACE "\\;"                                    " /wd" "GTKMM_DISABLE_SPECIFIC_WARNINGS" "${GTKMM_DISABLE_SPECIFIC_WARNINGS}")
		SET_TARGET_PROPERTIES(uftt-gtk 	 "${GTKMM_DISABLE_SPECIFIC_WARNINGS}")
	ENDIF(MSVC)

ENDIF(ENABLE_GTK_GUI)
